<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>36. Our last sequence</title>
</head>
<body>
<h2 class="title">36. Our last sequence</h2>
<p>Lists and strings can both be <i>sliced</i>, using the <tt>[::]</tt> notation.  Individual items can be found through their <i>index</i>, that is with the notation <tt>[index]</tt>.  Since they have these properties, lists and strings are known as <i><u>sequences</u> data types.</i>  Python has other similar data types which are sequences; one of the most common ones,
and the last one we will see, is known as a <i>tuple</i>.</p> 
<!--==============================-->
<hr width="50%">
<h3 class="section">A comma is a powerful thing</h3>
<p>Remember when we tried to use a comma while writing a large number?  Here's another example:</p>
<pre>
&gt;&gt;&gt; 1,000
(1, 0)
</pre>
<p>Python has taken the two values separated by a comma to be the elements of a tuple.  When it writes out a tuple, Python puts parentheses around it to indicate that it is a single object.</p>
<p><span class="warning">Note:</span> if you get an error message while attempting to do an example similar to the one we showed (but with different numbers) or get a result you can not explain given what you see here, ignore it for now. We'll come back to it at the end of this lesson.</p> 
<p>Here's perhaps a more interesting example.</p>
<pre>
&gt;&gt;&gt; a = 1, 2, 3, 4, <span class="string">"enough!"</span>
&gt;&gt;&gt; <span class="keyword">print</span> a
(1, 2, 3, 4, <span class="string">'enough!'</span>)

&gt;&gt;&gt; a[0]
1

&gt;&gt;&gt; a[1:4]
(2, 3, 4)

&gt;&gt;&gt; a[-1]
<span class="string">'enough!'</span>
</pre>
<p>We can also put tuples inside tuples.  Here are two examples.</p>
<pre>
&gt;&gt;&gt; b = 1, 2, 3

&gt;&gt;&gt; c = 4, 5, b
&gt;&gt;&gt; <span class="keyword">print</span> c
(4, 5, (1, 2, 3))

&gt;&gt;&gt; d = 1, 2, (3, 4, 5)
&gt;&gt;&gt; <span class="keyword">print</span> d
(1, 2, (3, 4, 5))
</pre>
<p>Other than using parentheses () instead of square brackets [], it would <i>appear</i> that tuples and lists are the same.  However, they differ in a very important way:</p>
<pre>
&gt;&gt;&gt; my_tuple = 1, 2, 3
&gt;&gt;&gt; my_list = [1, 2, 3]

&gt;&gt;&gt; <span class="keyword">print</span> my_tuple
(1, 2, 3)
&gt;&gt;&gt; <span class="keyword">print</span> my_list
[1, 2, 3]

&gt;&gt;&gt; my_list[1] = <span class="string">"new"</span>
&gt;&gt;&gt; <span class="keyword">print</span> my_list
[1, <span class="string">'new'</span>, 3]

&gt;&gt;&gt; my_tuple[1] = <span class="string">"new"</span>
Traceback (most recent call last):
  File <span class="string">"&lt;input&gt;"</span>, line 1, in ?
TypeError: object does not support item assignment
</pre>
<p>Whereas we can change a list, by changing one of its element (or by adding to it), we can not change a tuple: it is said to be <i>immutable</i>.  In addition to numbers, an other immutable data type we have seen is the string:</p>
<pre>
&gt;&gt;&gt; my_string = <span class="string">"abcd"</span>
&gt;&gt;&gt; my_string[2]
<span class="string">'c'</span>
&gt;&gt;&gt; my_string[2] = <span class="string">"e"</span>
Traceback (most recent call last):
  File <span class="string">"&lt;input&gt;"</span>, line 1, in ?
TypeError: object does not support item assignment
</pre>
<p>Since tuples are immutable, it means that they can be used as keys in a dict!</p>
<!--==============================-->
<hr width="50%">
<h3 class="section">dict, lists and tuples</h3>
<p>We have seen before that Python keeps a list with the elements in the same order as we create them, but that it prints out a dict in what appears to be a random order.  We can see this happening in Reeborg's world.</p>

<p>Click on the "Show/Hide world file" button  
<img alt="Show/Hide world file"  src="../../images/intro/btn_show_world_file.png"> to display the world file on the right hand side.  Now, click on the edit wall button: <img alt="edit walls button"  src="../../images/intro/btn_walls.png">.  </p>
<p>Create a few walls, and watch what happens in the world file.  Each wall created is a list element whose value is a tuple with the world coordinate.  Each wall that is created is <i>appended</i> at the end of the list.  When you delete a wall, by clicking on an existing wall, it is removed from the list at that location.  If you immediately click again to recreate that wall, it appears in the list at the end, being appended again.</p>
<p>Contrast this with beepers.  You can right-click on any street corner to specify the number of beepers to put there.  If you change the value of the beepers at a given corner, they get changed in-place.  If you add beepers at a corner where there were none before, the new beepers are, in general, <b>not</b> appended at the end. (You may need to have more than a few corners with beepers to see this.)  Make sure you have many corners with beepers. Select a corner with beepers and set the value to zero, watching the world file.  Like for the walls, the entry disappears.  Now, immediately put some beepers back on that same corner.  The new entry appears at the original place, and not at the end like it was the case for the list.  This is because the order in which items appear in a dict is based on a mathematical function ("hash") of the dict "key" (the tuple with the world coordinate in this case) which Python knows how to use efficiently, but which makes it appear as random to us.</p>


<center><a href="35-dicts.htm"><img alt="previous" src=
"../../images/previous.png">An other kind of definition</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"37-class.htm">Fixing up Reeborg.<img alt="next"
src="../../images/next.png"></a></center>
</body>
</html>
